package top.zsyle.user.service.impl;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import top.zsyle.basic.config.BussinessException;
import top.zsyle.basic.constant.RegisterConst;
import top.zsyle.user.domain.Logininfo;
import top.zsyle.user.domain.User;
import top.zsyle.user.dto.EmailRegisterDto;
import top.zsyle.user.dto.PhoneRegisterDto;
import top.zsyle.user.mapper.LogininfoMapper;
import top.zsyle.user.mapper.UserMapper;
import top.zsyle.user.service.IUserService;
import top.zsyle.basic.service.impl.BaseServiceImpl;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zsy
 * @since 2022-11-24
 */
@Service
public class UserServiceImpl extends BaseServiceImpl<User> implements IUserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private LogininfoMapper logininfoMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 手机注册
     * @param dto
     */
    @Override
    public void phoneRegister(PhoneRegisterDto dto) {
        //1.校验参数
        //2.检验手机号是否已经注册
        User user = userMapper.findByPhone(dto.getPhone());
        if (null != user){
            throw new BussinessException("您电话已经注册过了，快去登录吧!");
        }
        //3.判断两次密码是否一致
        if (!dto.getPassword().equals(dto.getConfirmPassword())){
            throw new BussinessException("两次密码必须一样!");
        }
        //4.判断手机验证码是否已经过期
        String smsCodeRedis = (String) redisTemplate.opsForValue().get(RegisterConst.REGISTER_PHONE_KEY + dto.getPhone());
        if (null == smsCodeRedis){
            throw new BussinessException("您的短信验证码已经过期了，请重新获取");
        }
        //5.判断手机验证码是否正确
        if (!smsCodeRedis.equals(dto.getSmsCodeValue())){
            throw new BussinessException("您的短信验证码不正确，请重新输入");
        }
        //6.将信息保存到 logininfo 表和 user 表中
        user = dto.getUser();
        saveUserAndLogininfo(user);
    }

    /**
     * 邮箱注册
     * @param dto
     */
    @Override
    public void emailRegister(EmailRegisterDto dto) {
        //1.校验参数
        //2.检验邮箱是否已经注册
        User user = userMapper.findByEmail(dto.getEmail());
        if (null != user){
            throw new BussinessException("您的邮箱已经注册过了，快去登录吧!");
        }
        //3.判断两次密码是否一致
        if (!dto.getPassword().equals(dto.getConfirmPassword())){
            throw new BussinessException("两次密码不一致");
        }
        //4.判断邮箱验证码是否已经过期
        String emailCodeRedis = (String) redisTemplate.opsForValue().get(String.format(RegisterConst.REGISTER_EMAIL_KEY, dto.getEmail()));
        if (null == emailCodeRedis){
            throw new BussinessException("验证码已过期，请从新获取");
        }
        //5.判断邮箱验证码是否正确
        if (!emailCodeRedis.equals(dto.getEmailCodeValue())){
            throw new BussinessException("验证码不正确");
        }
        //6.将信息保存到 logininfo 表和 user 表中
        user = dto.getUser();
        saveUserAndLogininfo(user);
    }

    /**
     * 将信息保存到 logininfo 表和 user 表中
     * @param user
     */
    private void saveUserAndLogininfo(User user) {
        Logininfo logininfo = new Logininfo();
        //将user中与logininfo相同字段赋值给logininfo
        BeanUtils.copyProperties(user,logininfo);
        //1 用户  0 员工
        logininfo.setType(1);
        //是否启用 1启用 0停用
        logininfo.setDisable(1);
        //添加到logininfo表
        logininfoMapper.add(logininfo);
        user.setLogininfoId(logininfo.getId());
        //添加到user表
        userMapper.add(user);
    }
}
